#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    timerId_1 = startTimer(1000);
    timerId_2 = startTimer(2000);
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key::Key_A)
    {
        qDebug() << "Widget push Key_A";
    }
    if(event->modifiers() == Qt::ControlModifier)
    {
        if(event->key() == Qt::Key_A)
        {
           qDebug() << "Widget push Ctrl + Key_A";
        }
    }
}

//计时器1每秒都会触发，而计时器2每两秒触发一次
void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == timerId_1)
    {
        int num = ui->label->text().toInt();
        ui->label->setText(QString::number(num+1));
        if(num+1 == 10)
        {
            killTimer(event->timerId());
        }
    }

    if(event->timerId() == timerId_2)
    {
        int num = ui->label_2->text().toInt();
        ui->label_2->setText(QString::number(num+1));
        if(num+1 == 10)
        {
            killTimer(event->timerId());
        }
    }
}

Widget::~Widget()
{
    delete ui;
}

